package org.centsuse.intention_spring.invention.config.scenes.processor;

import lombok.Data;
import lombok.ToString;
import org.centsuse.intention_spring.invention.Exception.ProcessingException;
import org.centsuse.intention_spring.invention.entities.contexts.DialogueContext;
import org.centsuse.intention_spring.invention.config.scenes.SceneSlotTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @description 具体某个场景的处理器
 * @param <T>
 */
@Data
@ToString
public abstract class AbstractSceneProcessor<T extends DialogueContext> implements SceneProcessor {

    // 通用槽位校验方法
    protected void validateRequiredSlots(T context, String... requiredSlots)
            throws ProcessingException {
        if (null == requiredSlots) {
            return;
        }
        for (String slot : requiredSlots) {
            if (!context.getSlots().containsKey(slot)) {
                throw new ProcessingException("缺少必要参数: " + slot);
            }
        }
    }

    // 安全获取槽位值
    protected <V> V getSafeSlotValue(T context, String slotKey, Class<V> valueType)
            throws ProcessingException {
        try {
            return context.getSlotValue(slotKey, valueType);
        } catch (ClassCastException e) {
            throw new ProcessingException("参数类型错误: " + slotKey);
        }
    }
}
